using System;
using System.Data;
using System.Collections;
using APP.QUANLY.Entity;
using APP.QUANLY.Entity.ConditionForm;

namespace APP.QUANLY.DAL
{
    /// <summary>
    /// Summary description for NGUOIDUNG_QUYENHAN.
     /// </summary>
	public class NGUOIDUNG_QUYENHANDao
    {
	    private string _SQLSelectByKey;
	    private string _SQLUpdate;
	    private string _SQLInsert;
	    private string _SQLDelete;
	    private string _SQLSelectAll;

        #region Properties

        public IDbConnection _dbConnection;

        public IDbConnection DbConnection
        {
            get { return _dbConnection; }
            set { _dbConnection = value; }
        }

        #endregion
        
        #region Constructor

        public NGUOIDUNG_QUYENHANDao()
        {
            _dbConnection = null;
		    InitSqlQuery();
        }

        public NGUOIDUNG_QUYENHANDao(IDbConnection con)
        {
            _dbConnection = con;
            InitSqlQuery();
        }

        #endregion


        private void ToLowerQuery()
        {
            _SQLDelete = _SQLDelete.ToLower();
            _SQLUpdate = _SQLUpdate.ToLower();
            _SQLInsert = _SQLInsert.ToLower();
            _SQLSelectByKey = _SQLSelectByKey.ToLower();
        }

        private void ToUpperQuery()
        {
            _SQLDelete = _SQLDelete.ToUpper();
            _SQLUpdate = _SQLUpdate.ToUpper();
            _SQLInsert = _SQLInsert.ToUpper();
            _SQLSelectByKey = _SQLSelectByKey.ToUpper();
        }

        #region InitSqlQuery
        private void InitSqlQuery()
       {
            string query;

            query = @"SELECT MANGUOIDUNG, MACHUCNANG, QUYENXEM, QUYENSUA, QUYENXOA, QUYENTIMKIEM, QUYENXUATFILE, QUYENIN FROM NGUOIDUNG_QUYENHAN WHERE (MANGUOIDUNG = @MANGUOIDUNG AND 
MACHUCNANG = @MACHUCNANG)";
	        _SQLSelectByKey = query;

            query = @"UPDATE NGUOIDUNG_QUYENHAN SET QUYENXEM = @QUYENXEM, QUYENSUA = @QUYENSUA, QUYENXOA = @QUYENXOA, QUYENTIMKIEM = @QUYENTIMKIEM, QUYENXUATFILE = @QUYENXUATFILE, QUYENIN = @QUYENIN WHERE (MANGUOIDUNG = @MANGUOIDUNG AND 
MACHUCNANG = @MACHUCNANG)";
	        _SQLUpdate = query;

            query = @"INSERT INTO NGUOIDUNG_QUYENHAN VALUES (@MANGUOIDUNG, @MACHUCNANG, @QUYENXEM, @QUYENSUA, @QUYENXOA, @QUYENTIMKIEM, @QUYENXUATFILE, @QUYENIN)";
	        _SQLInsert = query;

            query = @"DELETE FROM NGUOIDUNG_QUYENHAN WHERE (MANGUOIDUNG = @MANGUOIDUNG AND 
MACHUCNANG = @MACHUCNANG)";
	        _SQLDelete = query;

            query = @"SELECT MANGUOIDUNG, MACHUCNANG, QUYENXEM, QUYENSUA, QUYENXOA, QUYENTIMKIEM, QUYENXUATFILE, QUYENIN FROM NGUOIDUNG_QUYENHAN";
	        _SQLSelectAll = query;
        }
        #endregion
        public void LoadByCommandText(string commandtext, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = commandtext;

				if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        #region Save & Delete
        private void Save(NGUOIDUNG_QUYENHAN obj, IDbTransaction trans, bool isExisting)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                command = _dbConnection.CreateCommand();

                if (!isExisting) { command.CommandText = _SQLInsert + ";"; }
                else { command.CommandText = _SQLUpdate + ";"; }

                         DalTools.addDbDataParameter(command, "MANGUOIDUNG", obj.MANGUOIDUNG, DbType.String);
         DalTools.addDbDataParameter(command, "MACHUCNANG", obj.MACHUCNANG, DbType.Int32);
                         DalTools.addDbDataParameter(command, "QUYENXEM", obj.QUYENXEM, DbType.Boolean);
         DalTools.addDbDataParameter(command, "QUYENSUA", obj.QUYENSUA, DbType.Boolean);
         DalTools.addDbDataParameter(command, "QUYENXOA", obj.QUYENXOA, DbType.Boolean);
         DalTools.addDbDataParameter(command, "QUYENTIMKIEM", obj.QUYENTIMKIEM, DbType.Boolean);
         DalTools.addDbDataParameter(command, "QUYENXUATFILE", obj.QUYENXUATFILE, DbType.Boolean);
         DalTools.addDbDataParameter(command, "QUYENIN", obj.QUYENIN, DbType.Boolean);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Delete(NGUOIDUNG_QUYENHAN obj, IDbTransaction trans)
        {
            if (_dbConnection == null)
            {
                //throw new DalException("Connection has not been set.");
                throw new Exception("Connection has not been set.");
            }

            IDbCommand command;

            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }

                command = _dbConnection.CreateCommand();
                command.CommandText = _SQLDelete + ";";

                         DalTools.addDbDataParameter(command, "MANGUOIDUNG", obj.MANGUOIDUNG, DbType.String);
         DalTools.addDbDataParameter(command, "MACHUCNANG", obj.MACHUCNANG, DbType.Int32);

		        if (trans != null) { command.Transaction = trans; }
                if (command.ExecuteNonQuery() == 0) { throw new Exception("Zero rows were affected."); }
            }
            catch (Exception ex) { throw ex; }
        }

        public void Insert(NGUOIDUNG_QUYENHAN obj, IDbTransaction trans)
        {
            Save(obj, trans, false);
        }
        public void Update(NGUOIDUNG_QUYENHAN obj, IDbTransaction trans)
        {
            Save(obj, trans, true);
        }
        #endregion

        #region Command Execute & Read Properties
     
        private NGUOIDUNG_QUYENHANList SelectListByCommand(IDbCommand command)
        {
            NGUOIDUNG_QUYENHANList list = new NGUOIDUNG_QUYENHANList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				NGUOIDUNG_QUYENHAN obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new NGUOIDUNG_QUYENHAN();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
		
        private NGUOIDUNG_QUYENHANList SelectListByCommand(IDbCommand command, IDbTransaction trans)
        {
            NGUOIDUNG_QUYENHANList list = new NGUOIDUNG_QUYENHANList();
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {	
				NGUOIDUNG_QUYENHAN obj;
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
				command.Transaction = trans;
                using (IDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read()) 
					{ 
						obj = new NGUOIDUNG_QUYENHAN();
						ReadProperties(obj, reader);
						list.Add(obj); 
					}
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        private void DeleteListByCommand(IDbCommand command, IDbTransaction trans)
        {
            if (_dbConnection == null) { throw new Exception("Connection has not been set."); }
            try
            {
                if (_dbConnection.State != ConnectionState.Open) { _dbConnection.Open(); }
                if (trans != null) { command.Transaction = trans; }
                command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
	
        private void ReadProperties(NGUOIDUNG_QUYENHAN obj, IDataReader reader)
        {
            try
            {
                                if(!(reader["MANGUOIDUNG"] is DBNull))
		{
			obj.MANGUOIDUNG = (string)reader["MANGUOIDUNG"];
		}
                if(!(reader["MACHUCNANG"] is DBNull))
		{
			obj.MACHUCNANG = (Int32)reader["MACHUCNANG"];
		}
                if(!(reader["QUYENXEM"] is DBNull))
		{
			obj.QUYENXEM = (bool)reader["QUYENXEM"];
		}
                if(!(reader["QUYENSUA"] is DBNull))
		{
			obj.QUYENSUA = (bool)reader["QUYENSUA"];
		}
                if(!(reader["QUYENXOA"] is DBNull))
		{
			obj.QUYENXOA = (bool)reader["QUYENXOA"];
		}
                if(!(reader["QUYENTIMKIEM"] is DBNull))
		{
			obj.QUYENTIMKIEM = (bool)reader["QUYENTIMKIEM"];
		}
                if(!(reader["QUYENXUATFILE"] is DBNull))
		{
			obj.QUYENXUATFILE = (bool)reader["QUYENXUATFILE"];
		}
                if(!(reader["QUYENIN"] is DBNull))
		{
			obj.QUYENIN = (bool)reader["QUYENIN"];
		}
            }
            catch (Exception ex)
            {
                throw new DalException("Failed to read properties from DataReader.", ex);
            }
        }
        #endregion
      
        public NGUOIDUNG_QUYENHAN SelectByKey(string MANGUOIDUNG, Int32 MACHUCNANG)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MANGUOIDUNG", MANGUOIDUNG, DbType.String);
         DalTools.addDbDataParameter(command, "MACHUCNANG", MACHUCNANG, DbType.Int32);
      	    NGUOIDUNG_QUYENHANList list =  SelectListByCommand(command);

            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }

        public NGUOIDUNG_QUYENHAN SelectByKey(string MANGUOIDUNG, Int32 MACHUCNANG, IDbTransaction trans)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectByKey;
	                 DalTools.addDbDataParameter(command, "MANGUOIDUNG", MANGUOIDUNG, DbType.String);
         DalTools.addDbDataParameter(command, "MACHUCNANG", MACHUCNANG, DbType.Int32);
      	    NGUOIDUNG_QUYENHANList list =  SelectListByCommand(command, trans);
			
            if (list.Count == 0) { throw new Exception("No data was returned"); }
            return list[0];
        }
		
        public NGUOIDUNG_QUYENHANList SelectAll()
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = _SQLSelectAll;
      	    NGUOIDUNG_QUYENHANList list =  SelectListByCommand(command);
	        return list;
        }

         public NGUOIDUNG_QUYENHANList SelectByCommand(string Command)
        {
            IDbCommand command  = _dbConnection.CreateCommand();
            command.CommandText = Command;
      	    NGUOIDUNG_QUYENHANList list =  SelectListByCommand(command);
	        return list;
        }

        public NGUOIDUNG_QUYENHANList SelectByCondition(NGUOIDUNG_QUYENHANConditionForm condt)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "SELECT * FROM NGUOIDUNG_QUYENHAN " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            NGUOIDUNG_QUYENHANList list = SelectListByCommand(command);
            return list;
        }

        private void DeleteByCondition(NGUOIDUNG_QUYENHANConditionForm condt, IDbTransaction trans)
        {
            IDbCommand command = _dbConnection.CreateCommand();
            command.CommandText = "DELETE FROM NGUOIDUNG_QUYENHAN " + GetCondtionQuery(condt) + ";";
            FillParamToCommand(command, condt);
            DeleteListByCommand(command, trans);
        }

        #region Make Condition Query & Fill Param from Condition to Command
        private string GetCondtionQuery(NGUOIDUNG_QUYENHANConditionForm obj)
        {
            if (obj == null) { return ""; }
            string s = "";
            	if (obj.IsSetMANGUOIDUNG)
	{
    	    s += " AND MANGUOIDUNG = @MANGUOIDUNG ";
	}
	if (obj.IsSetMACHUCNANG)
	{
    	    s += " AND MACHUCNANG = @MACHUCNANG ";
	}
	if (obj.IsSetQUYENXEM)
	{
    	    s += " AND QUYENXEM = @QUYENXEM ";
	}
	if (obj.IsSetQUYENSUA)
	{
    	    s += " AND QUYENSUA = @QUYENSUA ";
	}
	if (obj.IsSetQUYENXOA)
	{
    	    s += " AND QUYENXOA = @QUYENXOA ";
	}
	if (obj.IsSetQUYENTIMKIEM)
	{
    	    s += " AND QUYENTIMKIEM = @QUYENTIMKIEM ";
	}
	if (obj.IsSetQUYENXUATFILE)
	{
    	    s += " AND QUYENXUATFILE = @QUYENXUATFILE ";
	}
	if (obj.IsSetQUYENIN)
	{
    	    s += " AND QUYENIN = @QUYENIN ";
	}
            if (s.Length > 0)
            {
                s =" WHERE "+s.Substring(4);
                return s;
            }
            return "";
        }

        private void FillParamToCommand(IDbCommand command, NGUOIDUNG_QUYENHANConditionForm obj)
        {
	    if (obj == null) { return;}
            	            if (obj.IsSetMANGUOIDUNG)
            {
            	DalTools.addDbDataParameter(command, "MANGUOIDUNG", obj.MANGUOIDUNG, DbType.String); 
            }
            if (obj.IsSetMACHUCNANG)
            {
            	DalTools.addDbDataParameter(command, "MACHUCNANG", obj.MACHUCNANG, DbType.Int32); 
            }
            if (obj.IsSetQUYENXEM)
            {
            	DalTools.addDbDataParameter(command, "QUYENXEM", obj.QUYENXEM, DbType.Boolean); 
            }
            if (obj.IsSetQUYENSUA)
            {
            	DalTools.addDbDataParameter(command, "QUYENSUA", obj.QUYENSUA, DbType.Boolean); 
            }
            if (obj.IsSetQUYENXOA)
            {
            	DalTools.addDbDataParameter(command, "QUYENXOA", obj.QUYENXOA, DbType.Boolean); 
            }
            if (obj.IsSetQUYENTIMKIEM)
            {
            	DalTools.addDbDataParameter(command, "QUYENTIMKIEM", obj.QUYENTIMKIEM, DbType.Boolean); 
            }
            if (obj.IsSetQUYENXUATFILE)
            {
            	DalTools.addDbDataParameter(command, "QUYENXUATFILE", obj.QUYENXUATFILE, DbType.Boolean); 
            }
            if (obj.IsSetQUYENIN)
            {
            	DalTools.addDbDataParameter(command, "QUYENIN", obj.QUYENIN, DbType.Boolean); 
            }
        }
        #endregion
    }
}